<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "user_friend".
 *
 * @property int $id ID
 * @property int $userid 用户id
 * @property int $interest_id 关注用户id
 * @property int $addtime 添加时间
 */
class UserInterest extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user_interest';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userid', 'interest_id', 'addtime'], 'required'],
            [['userid', 'interest_id', 'addtime'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'userid' => '用户id',
            'interest_id' => '关注的用户id',
            'addtime' => '添加时间',
        ];
    }
    
    
    public static function checkIsInterested($userid,$interest_id){
        $sql = "select id from user_interest where userid=$userid and interest_id=$interest_id";
        $command = \Yii::$app->db->createCommand($sql);
        $data = $command->queryOne();
        if(!empty($data)){
            return 1;
        }else{
            return 0;
        }
    }
    
    public static function getInterestIds($userid){
        $sql = "select interest_id from user_interest where userid=$userid";
        $command = \Yii::$app->db->createCommand($sql);
        $interestdata = $command->queryAll();
        $data = [];
        foreach($interestdata as $interest_arr){
            array_push($data, $interest_arr['interest_id']);
        }
        return implode(',', $data);
    }
    
    public static function getFanNum($userid){
        $sql = "select count(id) fan_num from user_interest where interest_id=$userid";
        $command = \Yii::$app->db->createCommand($sql);
        $data = $command->queryOne();
        return $data['fan_num'];
    }
}
